// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Tails Object Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// Aliases
private alias 2 : TYPE_TAILSOBJECT

// Function declarations
reserve function TailsObject_Function61

function TailsObject_Function61

	switch object.value13
	case 0
		if object.frame == 0
			object.value12++
			object.value12 %= 40
		end if
		break
		
	case 1
	case 3
	case 4
		object.value12++
		object.value12 %= 40
		break
		
	case 7
	case 9
	case 10
	case 43
		object.value12++
		object.value12 %= 12
		break
		
	case 17
		object.value12++
		object.value12 %= 30
		break
		
	end switch
	
end function


event ObjectMain

	if stage.debugMode == true
	
		CallFunction(PlayerObject_ProcessPlayer)
		
		CheckEqual(options.attractMode, 0)
		temp0 = checkResult
		CheckEqual(inputPress.buttonB, 1)
		temp0 &= checkResult
		
		if temp0 == true
		
			object.type = TypeName[Debug Mode]
			object.yvel = 0
			object.state = PlayerObject_Blank
			object.frame = 0
			object.rotation = 0
			object.interaction = 0
			object.drawOrder = 4
			object.priority = PRIORITY_ACTIVE
			object.value8 = 0
			object.visible = 1
			object.value2 = 0
			object.value3 = 0
			object.value4 = 0
			object.frame = object.value17
			screen.cameraEnabled = 1
			screen.cameraStyle = 0
			StopSfx(SfxName[Flying])
			StopSfx(SfxName[Jump])
			if stage.state == 3
				stage.state = STAGE_RUNNING
			end if
			if object[1].type == TypeName[Player 2 Object]
				object[1].priority = PRIORITY_ACTIVE
			end if
			if object[+playerCount].propertyValue == 3
				object[+playerCount].type = TypeName[Invincibility]
				object[+playerCount].propertyValue = 0
			end if
		else
			if object.gravity == GRAVITY_GROUND
				object.value35 = 0
			end if
			CallFunction(object.state)
			ProcessAnimation()
			CallFunction(TailsObject_Function61)
			if object.animation == ANI_JUMPING
				screen.adjustCameraY = object.value30
			else
				if screen.adjustCameraY == object.value30
					screen.adjustCameraY = 0
					object.iypos += object.value30
				end if
			end if
			if object.value34 == 0
				temp0 = object.value42
				object.value42 = object.gravity
				ProcessObjectMovement()
				object.value42 ^= 1
				CheckEqual(object.gravity, GRAVITY_GROUND)
				object.value42 |= checkResult
				object.value42 ^= 1
				if temp0 == 1
					if object.value42 == 0
						object.value19 = 0
						achieveRingCount = 0
						if object.animation == ANI_JUMPING
							if object.down == 0
								if object.state != PlayerObject_HandleRoll
									if object.state != PlayerObject_Unknown2
										object.animation = ANI_WALKING
										if object.entityPos == screen.cameraTarget
											screen.adjustCameraY = 0
										end if
										object.iypos += object.value30
									end if
								end if
							end if
						end if
					end if
				end if
			else
				object.value34 = 0
			end if
		end if
	else
		CallFunction(PlayerObject_ProcessPlayer)
		if object.gravity == GRAVITY_GROUND
			object.value35 = 0
		end if
		CallFunction(object.state)
		ProcessAnimation()
		CallFunction(TailsObject_Function61)
		if object.animation == ANI_JUMPING
			screen.adjustCameraY = object.value30
		else
			if screen.adjustCameraY == object.value30
				screen.adjustCameraY = 0
				object.iypos += object.value30
			end if
		end if
		if object.value34 == 0
			temp0 = object.value42
			object.value42 = object.gravity
			ProcessObjectMovement()
			object.value42 ^= 1
			CheckEqual(object.gravity, GRAVITY_GROUND)
			object.value42 |= checkResult
			object.value42 ^= 1
			if temp0 == 1
				if object.value42 == 0
					object.value19 = 0
					achieveRingCount = 0
					if object.animation == ANI_JUMPING
						if object.down == 0
							if object.state != PlayerObject_HandleRoll
								if object.state != PlayerObject_Unknown2
									object.animation = ANI_WALKING
									if object.entityPos == screen.cameraTarget
										screen.adjustCameraY = 0
									end if
									object.iypos += object.value30
								end if
							end if
						end if
					end if
				end if
			end if
		else
			object.value34 = 0
		end if
	end if
	CallFunction(PlayerObject_ProcessSuperForms)
end event


event ObjectDraw

	if object.animation != object.prevAnimation
		object.prevAnimation = object.animation
		object.frame = 0
		object.animationTimer = 0
		object.animationSpeed = 0
	end if
	
	if object.value13 != object.animation
		if object.animation > 4
			object.value12 = 0
		end if
		if object.value13 > 4
			object.value12 = 0
		end if
		object.value13 = object.animation
	end if
	
	// Draw the Tails part of Tails
	switch object.value13
	case 0
		if object.frame == 0
			temp0 = object.value12
			temp0 >>= 3
			if object.visible == true
				DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
			end if
		end if
		break
		
	case 1
	case 3
	case 4
	case 37
		temp0 = object.value12
		temp0 >>= 3
		if object.visible == 1
			DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		end if
		break
		
	case 7
	case 9
		temp0 = object.value12
		temp0 >>= 2
		temp0 += 11
		if object.visible == 1
			DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		end if
		break
		
	case 10
	case 43
		temp0 = object.value12
		temp0 >>= 2
		CheckEqual(object.xvel, 0)
		temp1 = checkResult
		CheckEqual(object.yvel, 0)
		temp1 &= checkResult
		temp2 = object.rotation
		if temp1 == 0
			ATan2(object.rotation, object.xvel, object.yvel)
			object.rotation += 16
			object.rotation &= 255
			object.rotation >>= 5
			switch object.rotation
			case 0
			case 8
				temp0 += 5
				object.rotation = 0
				break
			case 1
				temp0 += 8
				if object.direction == FLIP_NONE
					object.rotation = 64
				else
					object.rotation = 0
				end if
				break
			case 2
				temp0 += 5
				object.rotation = 64
				break
			case 3
				temp0 += 8
				if object.direction == FLIP_NONE
					object.rotation = 128
				else
					object.rotation = 64
				end if
				break
			case 4
				temp0 += 5
				object.rotation = 128
				break
			case 5
				temp0 += 8
				if object.direction == FLIP_NONE
					object.rotation = 192
				else
					object.rotation = 128
				end if
				break
			case 6
				temp0 += 5
				object.rotation = 192
				break
			case 7
				temp0 += 8
				if object.direction == FLIP_NONE
					object.rotation = 0
				else
					object.rotation = 192
				end if
				break
			end switch
			if object.direction == FLIP_X
				object.rotation += 128
			end if
		else
			temp0 += 5
			object.rotation = 0
		end if
		object.rotation <<= 1
		if object.visible == 1
			DrawSpriteFX(temp0, FX_ROTATE, object.xpos, object.ypos)
		end if
		object.rotation = temp2
		break
		
	case 17
		temp0 = object.value12
		temp0 /= 10
		temp0 += 11
		if object.visible == 1
			DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		end if
		break
		
	end switch
	
	// Draw Tails himself
	// (Separate tails are drawn in the process above)
	DrawObjectAnimation()
	
end event


event ObjectStartup

	if stage.playerListPos == true
	
		LoadSpriteSheet("Players/Tails1.gif")
		
		LoadAnimation("Tails.ani")
		
		SpriteFrame(-22, -8, 16, 24, 82, 199)
		SpriteFrame(-26, -8, 20, 24, 99, 199)
		SpriteFrame(-26, -8, 20, 24, 120, 199)
		SpriteFrame(-26, -8, 20, 24, 141, 199)
		SpriteFrame(-26, -8, 20, 24, 162, 199)
		SpriteFrame(-35, -8, 24, 16, 231, 166)
		SpriteFrame(-35, -8, 24, 16, 231, 183)
		SpriteFrame(-35, -8, 24, 16, 231, 200)
		SpriteFrame(-25, 9, 20, 16, 235, 217)
		SpriteFrame(-25, 9, 18, 16, 237, 234)
		SpriteFrame(-25, 9, 20, 16, 216, 234)
		SpriteFrame(-30, -6, 24, 16, 231, 166)
		SpriteFrame(-30, -6, 24, 16, 231, 183)
		SpriteFrame(-30, -6, 24, 16, 231, 200)
		
	end if
	
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
